package com.tang.thepath;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.tang.thepath.model.RequestModel;
import com.tang.thepath.util.BitmapUtil;

import java.io.ByteArrayOutputStream;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class CustomDetailActivity extends BaseActivity {

    private TextView tvCompanyName;
    private TextView tvAddress;
    private TextView tvContact;
    private TextView tvPosition;
    private TextView tvMobile;
    private TextView tvCreatedAt;
    //private ImageView testIv;

    private LinearLayout imageContainer;


    protected View.OnTouchListener imageTouchListener = new View.OnTouchListener (){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_MOVE) {
                ((ImageView)v).clearColorFilter(); // 清除滤镜效果
            }
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ((ImageView)v).setColorFilter(Color.parseColor("#77000000")); // 设置滤镜效果
            }
            return false;
        }
    };

    protected View.OnClickListener imageClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ImageView iv = (ImageView) v;
            String url = iv.getTag().toString();
            Intent intent = new Intent(CustomDetailActivity.this,GalleryImagePreviewActivity.class);
            intent.putExtra("url",url);
            startActivity(intent);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_detail);

        tvCompanyName = (TextView) findViewById(R.id.tv_company_name);
        tvAddress = (TextView) findViewById(R.id.tv_address);
        tvContact = (TextView) findViewById(R.id.tv_contact);
        tvPosition = (TextView) findViewById(R.id.tv_position);
        tvMobile = (TextView) findViewById(R.id.tv_mobile);
        tvCreatedAt = (TextView) findViewById(R.id.tv_created_at);
        imageContainer = (LinearLayout) findViewById(R.id.image_list_layout);
        //testIv = (ImageView) findViewById(R.id.test_iv);

        Intent intent = getIntent();
        RequestModel.CustomInfo customInfo = (RequestModel.CustomInfo) intent.getSerializableExtra("customInfo");
        tvCompanyName.setText(customInfo.companyName);
        tvAddress.setText(customInfo.address);
        tvContact.setText(customInfo.contact);
        tvPosition.setText(customInfo.position);
        tvMobile.setText(customInfo.mobile);
        tvCreatedAt.setText(customInfo.createdAt.substring(0,16));

        setTitle(customInfo.contact);
        final LayoutInflater inflater = getLayoutInflater();
        showLoading("正在加载图片");
        Observable.from(customInfo.images)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, BitmapUrlHolder>() {

                    @Override
                    public BitmapUrlHolder call(String s) {
                        Bitmap bitmap =  BitmapUtil.getBitmapByUrl(s);
                        return new BitmapUrlHolder(s,bitmap);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BitmapUrlHolder>() {
                    @Override
                    public void onCompleted() {
                        dismisLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        //dismisLoading();
                        toast(e.getMessage());
                    }

                    @Override
                    public void onNext(BitmapUrlHolder bitmapUrlHolder) {
                        //ImageView iv = (ImageView) inflater.inflate(R.layout.detail_image_view,null);
                        ImageView iv = new ImageView(getContext());
                        LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(200, 200);
                        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        layoutParams.setMargins(10,10,10,10);
                        iv.setLayoutParams(layoutParams);
                        iv.setImageBitmap(bitmapUrlHolder.bitmap);
                        iv.setClickable(true);
                        iv.setOnTouchListener(imageTouchListener);
                        iv.setOnClickListener(imageClickListener);
                        iv.setTag(bitmapUrlHolder.url);
                        imageContainer.addView(iv);
                    }
                });

    }

    class BitmapUrlHolder {
        public String url;
        public Bitmap bitmap;

        public BitmapUrlHolder(String url,Bitmap bitmap){
            this.url = url;
            this.bitmap = bitmap;
        }
    }
}
